package com.base.themvx.utils

import android.graphics.Color
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.graphics.drawable.LayerDrawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.Px
import com.base.themvx.defNonSyncLazy
import com.base.themvx.dpToPx
import com.fee.thexselector.XSelector

/**
 ******************(^_^)***********************<br>
 * Author: fee(QQ/WeiXin:1176610771)<br>
 * Date: 2023/9/27<br>
 * Time: 11:15<br>
 * <P>DESC:
 * App 内通用的 背景资源构建者
 * </p>
 * ******************(^_^)***********************
 */
class CommonBgBuilder {

    companion object Builder {

        val mSp1Px by defNonSyncLazy {
            ScreenUtils.sp2px(1f).toFloat()
        }

        val mDp1Px by defNonSyncLazy {
            1.dpToPx()
        }


        val mDp1PxFloat by defNonSyncLazy {
            mDp1Px * 1f
        }


        val pageMainColor by defNonSyncLazy {
            0xFFF7F8FA.toInt()
        }


        val primaryBtnBg by defNonSyncLazy {
            XSelector.shapeItem()
                .corners(mDp1PxFloat * 15)
                .solidColor(0xFF99EE90.toInt())
        }


        /**
         * 构建两个图层的 LayerDrawable，主要用于构建有阴影的及前景的不同颜色的 LayerDrawable
         * @param radiusPx 圆角 PX
         * @param shadowColor 阴影的颜色
         * @param faceColor 前景的颜色
         * @param facePaddingBottom 前景的底边距离阴影背景的外边距
         * @return LayerDrawable
         */
        fun buildLayerDrawable(
            @Px radiusPx: Float,
            @ColorInt shadowColor: Int,
            @ColorInt faceColor: Int,
            @Px facePaddingBottom: Int,
            @Px facePaddingStart: Int = 0,
            @Px facePaddingEnd: Int = 0,
            @Px facePaddingTop: Int = 0
        ): LayerDrawable {
            //先绘制阴影层
            val builder = XSelector.shapeItem()
                .corners(radiusPx)
                .solidColor(shadowColor)
            val shadowBg = builder.build()
            val topLayer = builder.solidColor(faceColor)
                .build()
            val layerDrawable = LayerDrawable(arrayOf(shadowBg, topLayer))
//            if (facePaddingBottom > 0) {
//                layerDrawable.setLayerInsetBottom(1, facePaddingBottom)
//            }
            layerDrawable.setLayerInsetRelative(
                1,
                facePaddingStart,
                facePaddingTop,
                facePaddingEnd,
                facePaddingBottom
            )
            return layerDrawable
        }

        fun grayOrRegainView(isGrayOrRegain: Boolean, theView: View) {
            adjustSaturationOfView(
                if (isGrayOrRegain) {
                    0f
                } else 1f,
                theView
            )
        }

        fun adjustSaturationOfView(theSaturationValue: Float, theView: View) {
            val paint = Paint()
            val colorMatrix = ColorMatrix()
            colorMatrix.setSaturation(theSaturationValue)
            paint.colorFilter = ColorMatrixColorFilter(colorMatrix)
            theView.setLayerType(View.LAYER_TYPE_HARDWARE, paint)
        }
    }
}